import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Basic
import QtQuick.Layouts

// TextField - Input Mask
Rectangle {
    Layout.fillWidth: true
    Layout.fillHeight: true

    Column {
        spacing: 10
        anchors.centerIn: parent
        width: parent.width * 0.8

        Text {
            text: "请输入日期(YYYY-MM-DD):"
            font.pointSize: 10
        }

        TextField {
            id: maskedField
            width: parent.width * 0.8
            height: 30
            placeholderText: "请输入日期(YYYY-MM-DD)"
            inputMask: "9999-99-99"

            // 背景框
            background: Rectangle {
                border.color: "#c0c0c0"
                border.width: 1
                radius: 4
            }

            // 判断正确日期
            function isValidDate(str) {
                const fmt =Qt.formatDateTime(new Date(str), "yyyy-MM-dd");
                return fmt === str;
            }

            onTextChanged: {
                if (isValidDate(text)) {
                    logText.text = "正确日期：" + text;
                    logText.color = "green";
                } else {
                    logText.text = "错误日期：" + text;
                    logText.color = "red";
                }
            }
        }

        Text {
            id: logText
            width: parent.width
            elide: Text.ElideRight
            font.pointSize: 10
        }
    }
}
